package com.sxbbc.seller.service;

import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
import com.sxbbc.common.core.dto.jz.BExpVo;
import com.sxbbc.common.core.entity.jz.Address;
import com.sxbbc.common.core.service.IAddressService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 商家端 基础业务层 处理一些特有业务
 *
 * @author leehao
 * @date 2019-06-11 10:25
 */
@Service
public class BusinessBaseService {

    @Autowired
    private IAddressService addressService;

    /**
     * 处理运费模板地址数据
     * @param addr 省市的ID
     * @author leehao
     * @return java.util.List<java.lang.Integer>
     */
    public List<Address> handleExpAddress(String addr) {
        //得到地址分组的数据
        List<Integer> checkList = new ArrayList<>();
        String[] addrGroup = addr.split(",");  //根据省划分
        for (String group : addrGroup) {
            if (group.contains("::")) {
                String[] oneAddr = group.split("::");
                checkList.add(Integer.parseInt(oneAddr[0]));  //添加省
                String[] cities = oneAddr[1].split("~");
                for (String city : cities) {
                    checkList.add(Integer.parseInt(city));  //添加市
                }
            }
        }
        List<Address> addressList = addressService.queryAllAddress();
        for (Address address : addressList) {
            for (Address item : address.getCityList()) {
                if (!CollectionUtils.isEmpty(checkList) && checkList.size() > 0) {
                    for (Integer i : checkList) {
                        if (CalcuUtils.getInstance().isEquals(item.getId(),i)) {
                            item.setCheckFlag("checked");
                        }
                        if (CalcuUtils.getInstance().isEquals(item.getPid(),i)) {
                            address.setCheckFlag("checked");
                        }
                    }
                }
            }
        }
        return addressList;
    }

    /**
     * 新增运费模板时处理地址字符串
     * @param bExpVo
     * @author leehao
     * @return com.sxbbc.common.core.dto.jz.BExpVo
     */
    public BExpVo handleAddAddress(BExpVo bExpVo) {
        if (bExpVo.getAddrDetail() != null) {
            String[] pro = new String[bExpVo.getBeginNum().length];
            String[] cit = new String[bExpVo.getBeginNum().length];
            for (int i = 1; i < bExpVo.getAddrDetail().length; i++) {
                String oneAddr = bExpVo.getAddrDetail()[i];
                String[] addr = oneAddr.split(",");
                String proTemp = "";
                cit[i] = "";
                for (String s : addr) {
                    String cityTemp = "";
                    String[] split = s.split("::");
                    proTemp += split[0] + ",";
                    String[] cities = split[1].split("~");
                    for (String city : cities) {
                        cityTemp += city + ",";
                    }
                    cit[i] = cit[i] + cityTemp.substring(0,cityTemp.length()-1) + "^^";
                }
                pro[i] = proTemp.substring(0,proTemp.length()-1);
            }
            bExpVo.setProId(pro);
            bExpVo.setCityId(cit);
        }
        return bExpVo;
    }

    public String handleEditAddressShow(String proId, String cityId) {
        String str = "";
        if (StringUtils.isNotBlank(proId) && StringUtils.isNotBlank(cityId)) {
            String[] province = proId.split(",");
            String[] cities = cityId.split("\\^\\^");
            String[] group = new String[province.length];
            for (int i = 0; i < province.length; i++) {
                for (int j = 0; j < cities.length; j++) {
                    group[i] = province[i] + "::" + cities[i].replace(",","~");
                }
                str += group[i] + ",";
            }
        }
        return str;
    }
}
